// pages/goods_update/goods_update.js
import { request } from "../../request/index.js";
import { showModal, showToast, uploadFile, chooseImage, showLoading } from "../../util/AsyncWx.js";
let a = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsid: '',
    goods: {
      id: '',
      description: '',
      price: '',
      imgurl: '',
      contact: ''
    },
    descriptionLength: '',
    chooseImgs2: [],
    goodsInfo: [],


  },

  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    const { id } = options;
    if (a) {
      a = false;
      return
    } else {
      var goodsid = 'goods.id'
      this.setData({
        goodsid: id,
        [goodsid]: id
      })

      this.getGoodsInfoById(id);

    }


  },
  async getGoodsInfoById(id) {
    const result = await request({ url: "/goods/getGoodsById", data: { id } })
    this.setData({
      goodsInfo: result,
      descriptionLength: result[0].description.length
    })

  },

  descriptionInput: function (e) {
    var description = 'goods.description';
    this.setData({
      [description]: e.detail.value,
      descriptionLength: e.detail.value.length
    })
  },
  priceInput: function (e) {
    var price = 'goods.price'
    this.setData({
      [price]: e.detail.value
    })
  },
  contactInput: function (e) {
    var contact = 'goods.contact'
    this.setData({
      [contact]: e.detail.value
    })
  },
  handleImagePreview: function (e) {
    a = true
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },
  async handleChooseImg2() {
    a = true
    //调用小程序内置的选择图片api
    if (this.data.chooseImgs2.length < 1) {
      const res = await chooseImage({});
      this.setData({
        //图片数组 进行拼接
        chooseImgs2: res
      })
    } else {
      showToast({ title: "只能选择一张照片" })
    }

  },
  handleImagePreview2(e) {
    a = true
    const idx = e.target.dataset.idx
    const images = this.data.chooseImgs2
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },
  handleRemoveImg2(e) {
    //获取被点击组件的索引
    const { index } = e.currentTarget.dataset;
    // 获取data中的图片数组
    let { chooseImgs2 } = this.data
    // 删除元素
    chooseImgs2.splice(index, 1);
    this.setData({
      chooseImgs2
    })

  },
  async formSubmit(e) {
    var that = this;
    var descriptionLength = that.data.descriptionLength
    if (descriptionLength > 255) {
      showToast({ title: "只能选择一张照片" })
    } else {
      const result = await showModal({ content: "确认修改吗？" });
      if (result.confirm) {
        that.updateGoods()
      }
      // wx.showModal({
      //   title: '确认发布商品',
      //   showCancel: true,
      //   cancelText: '取消',
      //   cancelColor: '#000000',
      //   confirmText: '确定',
      //   confirmColor: '#3CC51F',
      //   success: (result) => {
      //     if(result.confirm){
      //       that.updateGoods()
      //     }
      //   }
      // });
    }
  },
  async updateGoods() {
    var that = this
    var goods = that.data.goods
    var imgurl = 'goods.imgurl'
    var url = that.data.chooseImgs2[0];
    if (url != null) {
      const res = await uploadFile({ url: "/imageUpload", filePath: url });
      that.setData({
        [imgurl]: res.data
      })
    }
    showLoading({ title: "修改中..." })
    const result = await request({ url: "/goods/updateGoodsById", method: "POST", data: goods });
    wx.hideLoading();
    showToast({ title: "修改成功" });
    wx.navigateBack({
      delta: 1
    });
  },
})